#pragma once
#include <iostream>

using namespace std;

template<class K>
class BSTreeNode
{
public:
	BSTreeNode(const K& key)
		:_left(nullptr)
		,_right(nullptr)
		,_key(key)
	{}
	BSTreeNode<K>* _left;
	BSTreeNode<K>* _right;
	K _key;
};

template<class K>
class BSTree
{
	typedef BSTreeNode<K> Node;
public:
	bool Insert(const K& key)
	{
		if (_root == nullptr)
		{
			_root = new Node(key);
			return true;
		}
		Node* cur = _root;
		Node* pre = _root;
		while (cur)
		{
			if (key > cur->_key)
			{
				pre = cur;
				cur = cur->_right;
			}
			else if (key < cur->_key)
			{
				pre = cur;
				cur = cur->_left;
			}
			else return false;
		}
		if (key > pre->_key)
		{
			pre->_right = new Node(key);
		}
		else
		{
			pre->_left = new Node(key);
		}
		return true;
	}

	bool Erase(const K& key)
	{
		Node* parent = nullptr;
		Node* cur = _root;
		while (cur)
		{
			if (cur->_key < key)
			{
				parent = cur;
				cur = cur->_right;
			}
			else if (cur->_key > key)
			{
				parent = cur;
				cur = cur->_left;
			}
			else
			{
				if (cur->_left == nullptr)
				{
					if (cur == _root)
					{
						_root = cur->_right;
					}
					else
					{
						if (parent->_left == cur)
						{
							parent->_left = cur->_right;
						}
						else
						{
							parent->_right = cur->_right;
						}
					}
					delete cur;
				}
				else if (cur->_right == nullptr)
				{
					if (cur == _root)
						_root = cur->_left;
					else
					{
						if (cur == parent->_left)
							parent->_left = cur->_left;
						else
							parent->_right = cur->_left;
					}
					delete cur;
				}
				else
				{
					Node* subleft = cur->_right;
					Node* parent = cur;
					while (subleft->_left)
					{
						parent = subleft;
						subleft = subleft->_left;
					}

					swap(cur->_key, subleft->_key);
					if (parent->_left == subleft)
						parent->_left = subleft->_right;
					else
					{
						parent->_right = subleft->_right;
					}
					delete subleft;
				}

				return true;
			}
		}

		return false;
	}
	bool Find(const K& key)
	{
		Node* cur = _root;
		while (cur)
		{
			if (cur->_key < key)
				cur = cur->_right;
			else if(cur->_key > key)
				cur = cur->left;
			else return true;
		}
		return false;
	}
	void InOrder()
	{
		_InOrder(_root);
		cout << endl;
	}
	bool FindR(const K& key)
	{
		return _FindR(_root, key);
	}
	bool InsertR(Node* root, const K& key)
	{
		return _InsertR(root, key);
	}

	bool EraseR(const K& key)
	{
		return _EraseR(_root, key);
	}

	BSTree() = default;

	~BSTree()
	{
		Destroy(_root);
	}

	BSTree(const BSTree<K>& t)
	{
		_root = Copy(t._root);
	}

	BSTree<K>& operator=(BSTree<K> t)
	{
		swap(_root, t._root);
		return *this;
	}
private:

	Node* Copy(Node* root)
	{
		if (root == nullptr)
			return nullptr;
		Node* newRoot = new Node(root->_key);
		newRoot->_left = Copy(root->_left);
		newRoot->_right = Copy(root->_right);
		return newRoot; 
	}
	void Destroy(Node*& root)
	{
		if (root == nullptr)
			return;

		Destroy(root->_left);
		Destroy(root->_right);
		delete root;
		root = nullptr;
		
	}
	Node* _root = nullptr;
	void _InOrder(Node* _root)
	{
		if (_root == nullptr)
		{
			return;
		}
		_InOrder(_root->_left);
		cout << _root->_key << ' ';
		_InOrder(_root->_right);
	}

	bool _FindR(Node* root, const K& key)
	{
		if (root == nullptr)
		{
			return false;
		}

		if (root->_key < key)
		{
			return _FindR(root->_right, key);
		}
		else if (root->_key > key)
		{
			return FindR(root->_left, key);
		}
		else return true;
	}

	bool _InsertR(Node*& root, const K& key)
	{
		if (root == nullptr)
		{
			root = new Node(key);
			return true;
		}
		else if (key > root->_key)
		{
			return InsertR(_root->_right, key);
		}
		else if (key < root->_key)
		{
			return InsertR(_root->_left, key);
		}
		return false;
	}

	bool _EraseR(Node*& root, const K& key)
	{
		if (root == nullptr)
			return false;

		if (root->_key < key)
			return _EraseR(root->_right, key);
		else if (root->_key > key)
			return _EraseR(root->_left, key);
		else
		{
			if (root->_left == nullptr)
			{
				Node* del = root;
				root = root->_right;
				delete del;
				return true;
			}
			else if (root->_right == nullptr)
			{
				Node* del = root;
				root = root->_left;
				delete del;
				return true;
			}
			else
			{
				Node* subleft = root->_right;
				while (subleft->_left)
				{
					subleft = subleft->_left;
				}

				swap(root->_key, subleft->_key);

				return _EraseR(root->_right, key);
			}
		}
			
	}
};



namespace kv
{
	template<class K, class V>
	class BSTreeNode
	{
	public:
		BSTreeNode(const K& key, const V& value)
			:_left(nullptr)
			, _right(nullptr)
			, _key(key)
			, _value(value)
		{}
		BSTreeNode<K, V>* _left;
		BSTreeNode<K, V>* _right;
		K _key;
		V _value;
	};

	template<class K, class V>
	class BSTree
	{
		typedef BSTreeNode<K, V> Node;
	public:
		bool Insert(const K& key, const V& value)
		{
			if (_root == nullptr)
			{
				_root = new Node(key, value);
				return true;
			}
			Node* cur = _root;
			Node* pre = _root;
			while (cur)
			{
				if (key > cur->_key)
				{
					pre = cur;
					cur = cur->_right;
				}
				else if (key < cur->_key)
				{
					pre = cur;
					cur = cur->_left;
				}
				else return false;
			}
			if (key > pre->_key)
			{
				pre->_right = new Node(key, value);
			}
			else
			{
				pre->_left = new Node(key, value);
			}
			return true;
		}

		bool Erase(const K& key)
		{
			Node* parent = nullptr;
			Node* cur = _root;
			while (cur)
			{
				if (cur->_key < key)
				{
					parent = cur;
					cur = cur->_right;
				}
				else if (cur->_key > key)
				{
					parent = cur;
					cur = cur->_left;
				}
				else
				{
					if (cur->_left == nullptr)
					{
						if (cur == _root)
						{
							_root = cur->_right;
						}
						else
						{
							if (parent->_left == cur)
							{
								parent->_left = cur->_right;
							}
							else
							{
								parent->_right = cur->_right;
							}
						}
						delete cur;
					}
					else if (cur->_right == nullptr)
					{
						if (cur == _root)
							_root = cur->_left;
						else
						{
							if (cur == parent->_left)
								parent->_left = cur->_left;
							else
								parent->_right = cur->_left;
						}
						delete cur;
					}
					else
					{
						Node* subleft = cur->_right;
						Node* parent = cur;
						while (subleft->_left)
						{
							parent = subleft;
							subleft = subleft->_left;
						}

						swap(cur->_key, subleft->_key);
						if (parent->_left == subleft)
							parent->_left = subleft->_right;
						else
						{
							parent->_right = subleft->_right;
						}
						delete subleft;
					}

					return true;
				}
			}

			return false;
		}
		Node* Find(const K& key)
		{
			Node* cur = _root;
			while (cur)
			{
				if (cur->_key < key)
					cur = cur->_right;
				else if (cur->_key > key)
					cur = cur->_left;
				else return cur;
			}
			return nullptr;
		}
		void InOrder()
		{
			_InOrder(_root);
			cout << endl;
		}
		bool FindR(const K& key)
		{
			return _FindR(_root, key);
		}
		bool InsertR(Node* root, const K& key)
		{
			return _InsertR(root, key);
		}

		bool EraseR(const K& key)
		{
			return _EraseR(_root, key);
		}

		BSTree() = default;

		~BSTree()
		{
			Destroy(_root);
		}

		BSTree(const BSTree<K, V>& t)
		{
			_root = Copy(t._root);
		}

		BSTree<K, V>& operator=(BSTree<K, V> t)
		{
			swap(_root, t._root);
			return *this;
		}
	private:

		Node* Copy(Node* root)
		{
			if (root == nullptr)
				return nullptr;
			Node* newRoot = new Node(root->_key);
			newRoot->_left = Copy(root->_left);
			newRoot->_right = Copy(root->_right);
			return newRoot;
		}
		void Destroy(Node*& root)
		{
			if (root == nullptr)
				return;

			Destroy(root->_left);
			Destroy(root->_right);
			delete root;
			root = nullptr;

		}
		Node* _root = nullptr;
		void _InOrder(Node* _root)
		{
			if (_root == nullptr)
			{
				return;
			}
			_InOrder(_root->_left);
			cout << _root->_key << ' ' << _root->_value;
			_InOrder(_root->_right);
		}

		bool _FindR(Node* root, const K& key)
		{
			if (root == nullptr)
			{
				return false;
			}

			if (root->_key < key)
			{
				return _FindR(root->_right, key);
			}
			else if (root->_key > key)
			{
				return FindR(root->_left, key);
			}
			else return true;
		}

		bool _InsertR(Node*& root, const K& key)
		{
			if (root == nullptr)
			{
				root = new Node(key);
				return true;
			}
			else if (key > root->_key)
			{
				return InsertR(_root->_right, key);
			}
			else if (key < root->_key)
			{
				return InsertR(_root->_left, key);
			}
			return false;
		}

		bool _EraseR(Node*& root, const K& key)
		{
			if (root == nullptr)
				return false;

			if (root->_key < key)
				return _EraseR(root->_right, key);
			else if (root->_key > key)
				return _EraseR(root->_left, key);
			else
			{
				if (root->_left == nullptr)
				{
					Node* del = root;
					root = root->_right;
					delete del;
					return true;
				}
				else if (root->_right == nullptr)
				{
					Node* del = root;
					root = root->_left;
					delete del;
					return true;
				}
				else
				{
					Node* subleft = root->_right;
					while (subleft->_left)
					{
						subleft = subleft->_left;
					}

					swap(root->_key, subleft->_key);

					return _EraseR(root->_right, key);
				}
			}

		}
	};
}